<?php namespace App\Core\Repository\Sorts;

use Input;
use App\Core\Repository\Sort;

class InputDragSort extends Sort {
    
    public $inputOperation = 'operation';
    public $inputSelfId = 'id';
    public $inputTargetId = 'target_id';
    
    public function __construct($config){
        parent::__construct($config);
        $this->inputOperation = isset($config['input_operation']) ? $config['input_operation'] : 'operation';
        $this->inputSelfId = isset($config['input_self_id']) ? $config['input_self_id'] : 'id';
        $this->inputTargetId = isset($config['input_target_id']) ? $config['input_target_id'] : 'target_id';
    }
    
    //执行排序
    public function sort(){
        $operation = Input::get($this->inputOperation, '');
		$selfId = Input::get($this->inputSelfId, '');
		$targetId = Input::get($this->inputTargetId, '');
		if(empty($selfId) || !in_array($operation, ['append', 'prepend', 'cancel', 'before', 'after'])){
			throw new \Exception('parameter not validated');
		}
        if(in_array($operation, ['before', 'after']) && empty($targetId)){
            throw new \Exception('parameter not validated');
        }
        if($operation == 'append'){
            $this->append($selfId);
        }elseif($operation == 'prepend'){
            $this->prepend($selfId);
        }elseif($operation == 'cancel'){
            $this->cancel($selfId);
        }elseif($operation == 'before'){
            $this->before($selfId, $targetId);
        }elseif($operation == 'after'){
            $this->after($selfId, $targetId);
        }
    }
}